package com.wxg.file;

import java.io.File;

import org.junit.Ignore;
import org.junit.Test;

import net.mindview.util.Directory;

public class FileTest {

	
	
	/**
	 * 
2981
2
文件[ weblaf-core-2.1.1.jar.lastUpdated ]成功删除！
文件[ weblaf-core-2.1.1.pom ]成功删除！
文件[ weblaf-core-2.1.1.pom.sha1 ]成功删除！
文件[ _remote.repositories ]成功删除！
文件[ 2.1.1 ]成功删除！
文件[ weblaf-ui-2.1.1.jar.lastUpdated ]成功删除！
文件[ weblaf-ui-2.1.1.pom ]成功删除！
文件[ weblaf-ui-2.1.1.pom.sha1 ]成功删除！
文件[ _remote.repositories ]成功删除！
文件[ 2.1.1 ]成功删除！

	 */
	@Test
	public void test003(){
		String rootpath = null;
		rootpath = "E:/tools/maven/repository/repos";
		File rootdir = new File(rootpath);
		Directory.TreeInfo treeinfo = Directory.walk(rootdir, ".*lastUpdated$");
		System.out.println( treeinfo.dirs.size() );
		System.out.println( treeinfo.files.size() );
		for(File ff : treeinfo.files){
			deleteFileOrDir2(ff.getParentFile());
		}
	}
	
	
	@Test
	@Ignore
	public void test002(){
		String rootpath = null;
		rootpath = "E:/tools/maven/repository/repos";
		File rootdir = new File(rootpath);
		Directory.TreeInfo treeinfo = Directory.walk(rootdir, ".*lastUpdated$");
		System.out.println( treeinfo.dirs.size() );
		System.out.println( treeinfo.files.size() );
		for(File ff : treeinfo.files){
			deleteFileOrDir(ff.getParentFile());
		}
	}
	
	@Test
	@Ignore
	public void test001(){
		String filepath = null;
		filepath = "C:/wxg2/mygitbooks/mavenStudy/configuration/aa/11.txt";
		File file = new File(filepath);
		File parent = file.getParentFile();
		deleteFileOrDir(parent);
	}
	
	/**
	 * 
	 * @param file
	 */
	private void deleteFileOrDir(File file){
		if(file.isDirectory()){
			String name = file.getName();
			File[] subfiles = file.listFiles();
			for(File ff : subfiles){
				deleteFileOrDir(ff);
			}
			boolean flag = file.delete();
			if(flag){
				System.out.println("文件[ "+name+" ]成功删除！");
			}else{
				System.out.println("文件[ "+name+" ]删除失败！");
			}
		}else{
			String name = file.getName();
			boolean flag = file.delete();
			if(flag){
				System.out.println("文件[ "+name+" ]成功删除！");
			}else{
				System.out.println("文件[ "+name+" ]删除失败！");
			}
		}
	}
	
	private void deleteFileOrDir2(File file){
		if(file.isDirectory()){
			String name = file.getName();
			File[] subfiles = file.listFiles();
			for(File ff : subfiles){
				deleteFileOrDir(ff);
			}
			if(file.exists()){
				boolean flag = file.delete();
				if(flag){
					System.out.println("文件[ "+name+" ]成功删除！");
				}else{
					System.out.println("文件[ "+name+" ]删除失败！");
				}
			}
		}else{
			String name = file.getName();
			boolean flag = file.delete();
			if(flag){
				System.out.println("文件[ "+name+" ]成功删除！");
			}else{
				System.out.println("文件[ "+name+" ]删除失败！");
			}
		}
	}
	
	
}
